package com.easy.easylibdemo.ui.greendao.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.easy.easylibdemo.app.EasyApplication;
import com.easy.easylibdemo.gen.DaoMaster;
import com.easy.easylibdemo.gen.DaoSession;
import com.easy.easylibdemo.gen.FourBeanDao;
import com.easy.easylibdemo.gen.OneBeanDao;
//import com.easy.easylibdemo.gen.ThreeBeanDao;
import com.easy.easylibdemo.gen.ThreeBeanDao;
import com.easy.easylibdemo.gen.TwoBeanDao;
import com.easy.easylibdemo.ui.greendao.model.FourBean;
import com.easylib.base.BaseConfig;
import com.easylib.utils.LogUtils;

import org.greenrobot.greendao.database.Database;

/**
 * 作　　者: guyj
 * 修改日期: 2017/2/7
 * 描　　述:
 * 备　　注:
 */
public class GreenDaoUtils {

    /**
     * singleton mode
     */
    private static class SingleTon{
        private static GreenDaoUtils getInstance=new GreenDaoUtils();
    }
    public static GreenDaoUtils getInstance() {
        return SingleTon.getInstance;
    }
    private GreenDaoUtils() {
//        openHelper = new DaoMaster.DevOpenHelper(BaseConfig.application, dbName, null);
        openHelper = new MyOpenHelper(BaseConfig.application, dbName, null);
    }

    private final static String dbName="easy.db";
//    private DaoMaster.DevOpenHelper openHelper;
    private MyOpenHelper openHelper;
    private DaoMaster daoMaster;
    private DaoSession daoSession;


    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
//            openHelper = new DaoMaster.DevOpenHelper(BaseConfig.application, dbName, null);
            openHelper = new MyOpenHelper(BaseConfig.application, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

    private DaoMaster getDaoMaster(){
        if (daoMaster==null){
            daoMaster = new DaoMaster(getWritableDatabase());
        }
        return daoMaster;
    }

    public DaoSession getDaoSession(){
        if (daoSession==null){
            daoSession=getDaoMaster().newSession();
        }
        return daoSession;
    }

    /**
     * Close any open database object.
     */
    public void closeGreenDao(){
        openHelper.close();
    }

    /**
     * 自定义Helper类重写升级的方法，通过for循环解决跨版本升级数据库的需求
     */
    class MyOpenHelper extends DaoMaster.OpenHelper{

        public MyOpenHelper(Context context, String name) {
            super(context, name);
        }

        public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
            super(context, name, factory);
        }

        @Override
        public void onUpgrade(Database db, int oldVersion, int newVersion) {
            super.onUpgrade(db, oldVersion, newVersion);
            for (int i = oldVersion; i < newVersion; i++) {
                switch (i) {
                    case 1:
                        upgradeToVersion2(db);
                        break;
                    case 2:
                        upgradeToVersion3(db);
                        break;
                    default:
                        break;
                }
            }
        }

        private void upgradeToVersion3(Database db) {
            LogUtils.e("GreenDaoUtils","==创建FourBeanDao==");
            FourBeanDao.createTable(db,true);
        }

        private void upgradeToVersion2(Database db) {
            LogUtils.e("GreenDaoUtils","==ThreeBeanBao添加lastname字段==");
            db.execSQL("alter table '"+ThreeBeanDao.TABLENAME+"' add 'lastname' TEXT;");
        }
    }

}
